package com.example.openthedoor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.openthedoor.databinding.LoginFragmentBinding;

public class LoginFragment extends Fragment {
    LoginFragmentBinding binding;
    private MyViewModel myViewModel;
    private String userName, passWord;



    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.login_fragment, container, false);
        binding.setData(myViewModel);

        binding.buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = binding.editTextName.getText().toString();
                passWord = binding.editTextPassword.getText().toString();
                myViewModel.login(userName,passWord);
                myViewModel.delay(300);
                // 查询数据库返回结果是否成功
                if (myViewModel.getSuccess()) {
                    //初始化用户人脸库
                    //myViewModel.init();
                    NavController controller = Navigation.findNavController(v);
                    controller.navigate(R.id.action_loginFragment_to_doorFragment);
                } else if (userName.isEmpty() || passWord.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
                    builder.setTitle("请输入用户名或密码！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
                    builder.setTitle("用户名或密码错误！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });

        binding.buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_loginFragment_to_registerFragment);
            }
        });


        return binding.getRoot();
    }
}